package com.rpay.admin.config.security;

import com.rpay.model.admin.RpayAdminUser;
import com.rpay.model.admin.RpayAdminUserRole;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class MyUserDetails extends RpayAdminUser implements UserDetails {
    private RpayAdminUser user;

    private List<RpayAdminUserRole> roles;

    public MyUserDetails(RpayAdminUser user, List<RpayAdminUserRole> roles) {
        super(user);
        this.roles = roles;
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (roles == null || roles.size() < 1) {
            return AuthorityUtils.commaSeparatedStringToAuthorityList("");
        }
        StringBuilder commaBuilder = new StringBuilder();
        for (RpayAdminUserRole role : roles) {
            commaBuilder.append(role.getRoleId()).append(",");
        }
        String authorities = commaBuilder.substring(0, commaBuilder.length() - 1);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public String getUsername() {
        return super.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public RpayAdminUser getUser() {
        return user;
    }

    public void setUser(RpayAdminUser user) {
        this.user = user;
    }
}